from django import forms
from django.core.validators import RegexValidator
# 创建邮箱正则校验器
email_validator=RegexValidator(r"[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?",message='邮箱格式不正确')
class BoardForm(forms.Form):
    contactname = forms.CharField(label='姓名', max_length=20,
                                  error_messages={
                                      'max_length': '姓名长度要小于20',
                                      'required': '姓名不能为空'
                                  })
    email = forms.CharField(label='邮箱',min_length=6,
                            max_length=30,
                            validators=[email_validator, ],
                            error_messages={
                                'max_length': '邮箱长度有误',
                                'min_length': '邮箱长度有误',
                                'required': '邮箱不能为空'
                            }, )
    message=forms.CharField(label='内容',min_length=10,
                            error_messages={
                                'min_length': '内容输入不够',
                                'required': '内容不能为空'
                            }, )

    def clean(self):
        # 获取邮箱
        contactname = self.data.get('contactname')
        email = self.data.get('email')
        content = self.data.get('message')
        return contactname,email,content